using System;
using System.Collections.Generic;

namespace UnitTesting.Fixturing
{
	class SimpleTypeSequential : ISimpleTypeGenerator
	{
		readonly Dictionary<Type, int> _generators = new Dictionary<Type, int>();

		internal int InclusiveMin { get; private set; }

		internal int ExclusiveMax { get; private set; }

		internal SimpleTypeSequential()
			: this(SimpleValueProvider.INCLUSIVE_MIN, SimpleValueProvider.EXCLUSIVE_MAX)
		{
		}

		internal SimpleTypeSequential(int inclusiveMin, int exclusiveMax)
		{
			InclusiveMin = inclusiveMin;
			ExclusiveMax = exclusiveMax;
			SimpleValueProvider.EnsureIsValidRange(InclusiveMin, ExclusiveMax);
		}

		public int Next(Type simpleType)
		{
			Ensure.IsNotNull(() => simpleType);
			AddMinValue(simpleType);
			return GetNumber(simpleType);
		}

		void AddMinValue(Type type)
		{
			if (!_generators.ContainsKey(type))
			{
				_generators.Add(type, InclusiveMin);
			}
		}

		int GetNumber(Type simpleType)
		{
			if (_generators[simpleType] == ExclusiveMax)
			{
				ResetToMinValue(simpleType);
			}
			return _generators[simpleType]++;
		}

		void ResetToMinValue(Type simpleType)
		{
			_generators[simpleType] = InclusiveMin;
		}
	}
}